package com.openbravo.pos.panels.shard;

import java.util.Properties;

import com.openbravo.bean.shard.AddressEntity;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.user.DirtyManager;
import com.openbravo.format.Formats;
import com.openbravo.pos.base.AppLocal;

import cn.ekuma.data.ui.swing.AbstractDAOJEditor;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;
import java.awt.Dimension;

public class AddressEditor extends AbstractDAOJEditor<AddressEntity>{
	private JTextField txtCountry;
	private JTextField txtRegion;
	private JTextField txtCity;
	private JTextField txtPostal;
	private JTextField txtAddress2;
	private JTextField txtAddress;
	private JLabel label_6;
	private JTextField txtPhone2;

	public AddressEditor(DirtyManager dirty) {
		super(dirty);
		setPreferredSize(new Dimension(450, 220));
		initComponents();
		txtAddress.getDocument().addDocumentListener(dirty);
        txtAddress2.getDocument().addDocumentListener(dirty);
        txtPostal.getDocument().addDocumentListener(dirty);
        txtCity.getDocument().addDocumentListener(dirty);
        txtRegion.getDocument().addDocumentListener(dirty);
        txtCountry.getDocument().addDocumentListener(dirty);
        txtPhone2.getDocument().addDocumentListener(dirty);
	}
	private void initComponents() {
		setBorder(new TitledBorder(null, AppLocal.getIntString("label.address"), TitledBorder.LEADING, TitledBorder.TOP, null, null));
		setLayout(null);
		
		JLabel label = new JLabel();
		label.setText(AppLocal.getIntString("label.address"));
		label.setBounds(10, 55, 140, 15);
		add(label);
		
		JLabel label_1 = new JLabel();
		label_1.setText(AppLocal.getIntString("label.address2"));
		label_1.setBounds(10, 82, 140, 15);
		add(label_1);
		
		JLabel label_2 = new JLabel();
		label_2.setText(AppLocal.getIntString("label.postal"));
		label_2.setBounds(10, 109, 140, 15);
		add(label_2);
		
		JLabel label_3 = new JLabel();
		label_3.setText(AppLocal.getIntString("label.city"));
		label_3.setBounds(10, 136, 140, 15);
		add(label_3);
		
		JLabel label_4 = new JLabel();
		label_4.setText(AppLocal.getIntString("label.region"));
		label_4.setBounds(10, 163, 140, 15);
		add(label_4);
		
		JLabel label_5 = new JLabel();
		label_5.setText(AppLocal.getIntString("label.country"));
		label_5.setBounds(10, 190, 140, 15);
		add(label_5);
		
		txtCountry = new JTextField();
		txtCountry.setBounds(154, 187, 270, 21);
		add(txtCountry);
		
		txtRegion = new JTextField();
		txtRegion.setBounds(154, 160, 270, 21);
		add(txtRegion);
		
		txtCity = new JTextField();
		txtCity.setBounds(154, 133, 270, 21);
		add(txtCity);
		
		txtPostal = new JTextField();
		txtPostal.setBounds(154, 106, 270, 21);
		add(txtPostal);
		
		txtAddress2 = new JTextField();
		txtAddress2.setBounds(154, 79, 270, 21);
		add(txtAddress2);
		
		txtAddress = new JTextField();
		txtAddress.setBounds(154, 52, 270, 21);
		add(txtAddress);
		
		label_6 = new JLabel();
		label_6.setText(AppLocal.getIntString("label.phone2"));
		label_6.setBounds(10, 27, 140, 15);
		add(label_6);
		
		txtPhone2 = new JTextField();
		txtPhone2.setBounds(154, 24, 270, 21);
		add(txtPhone2);
	}

	@Override
	public void refresh() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public AddressEntity createNew(Properties prop) {
		return new AddressEntity();
	}

	@Override
	public void rebuild(AddressEntity obj) throws BasicException {
		obj.setPhone2((String) Formats.STRING.parseValue(txtPhone2.getText())) ;
		obj.setAddress((String) Formats.STRING.parseValue(txtAddress.getText()));
		obj.setAddress2((String) Formats.STRING.parseValue(txtAddress2.getText())) ;
		obj.setPostal((String) Formats.STRING.parseValue(txtPostal.getText())) ;
		obj.setCity( (String) Formats.STRING.parseValue(txtCity.getText()));
		obj.setRegion((String) Formats.STRING.parseValue(txtRegion.getText()));
		obj.setCountry((String) Formats.STRING.parseValue(txtCountry.getText())) ;	
	}

	@Override
	public void updateUI(boolean editAble, int state) {
		txtPhone2.setText(Formats.STRING.formatValue(obj.getPhone2()));
		txtAddress.setText(Formats.STRING.formatValue(obj.getAddress()));
        txtAddress2.setText(Formats.STRING.formatValue(obj.getAddress2()));
        txtPostal.setText(Formats.STRING.formatValue(obj.getPostal()));
        txtCity.setText(Formats.STRING.formatValue(obj.getCity()));
        txtRegion.setText(Formats.STRING.formatValue(obj.getRegion()));
        txtCountry.setText(Formats.STRING.formatValue(obj.getCountry()));  
        
        txtPhone2.setEnabled(editAble);
        txtAddress.setEnabled(editAble);
        txtAddress2.setEnabled(editAble);
        txtPostal.setEnabled(editAble);
        txtCity.setEnabled(editAble);
        txtRegion.setEnabled(editAble);
        txtCountry.setEnabled(editAble);
	}

	@Override
	public Class getEditClass() {
		// TODO Auto-generated method stub
		return null;
	}
}
